var data = ["7", 3, 8, "13", 7, 9, "22", 3, 7, 9, "5", 8, "11", 3, 7, 9, "7", 8];

var transform = function (data) {
    var arr = data.reduce((acc, item) => {
        if (typeof item === "string") {
            acc.group = item;
        } else {
            acc.push([Number(acc.group), item]);
        }
        return acc;
    }, []);
    let index = 0;
    let result = [];
    let isExist = (arr, data) => {
        return arr.find(item => {
            return item && item[1] && data[1] && item[1] === data[1]
        });
    };
    let addToArr = (acc, index, item) => {
        if (!acc[index]) {
            acc[index] = [];
        }
        if (isExist(acc[index], item)) {
            addToArr(acc, ++index, item);
        } else {
            acc[index].push(item);
        }
    };
    arr.forEach(item => {
        addToArr(result, index, item);
    });
    result = result
        .map(item => {
            return item.sort((a, b) => {
                return a[1] - b[1];
            });
        })
        .reduce((acc, item) => {
            return acc.concat(item);
        }, []);
    return result;
};

console.log(transform(data));

